package ikelib;

import java.awt.AWTEvent;
import java.awt.Color;
import java.awt.Component;
import java.awt.Graphics;
import java.awt.Insets;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.border.AbstractBorder;
import javax.swing.border.Border;
import javax.swing.border.CompoundBorder;

/**
 * メニュー付きのボタン
 * 
 * @author Toshiharu Ikeda <ikeda.104@gmail.com>
 *
 */
public class JButtonWithMenu extends JButton{

	/**
	 * 
	 */
	private boolean clickDropDown=false;
	/**
	 * Whether Drop down triangle was clicked  
	 * @return
	 */
	public boolean isClickedDropDown(){
		return 	clickDropDown;

	}
	private static final long serialVersionUID = 1L;
	public JButtonWithMenu(){
		super();
		enableEvents(AWTEvent.MOUSE_EVENT_MASK);
	}
	public void setText(String text){
		Insets i = getMargin();
		i.right += 16;
		setMargin(i);
		super.setText(text);
		i.right = 0;
		i.left = 0;
		setMargin(i);
		
		Border border=getBorder();
		CompoundBorder cb = BorderFactory.createCompoundBorder(border,new PullDownBorder());
		setBorder(cb);
	}
    static class PullDownBorder extends AbstractBorder {
        /**
		 * 
		 */
		private static final long serialVersionUID = 1L;
		static final int WIDTH = 16;

        public void paintBorder(Component c, Graphics g, int x, int y, int w, int h) {
            g.translate(x+w-WIDTH,y-1);

            // down arrow
            int w2 = WIDTH/2;
            int h2 = h/2;
            g.setColor(Color.black);

            g.drawLine(w2-5,h2-2,w2+4,h2-2);
            g.drawLine(w2-4,h2-1,w2+3,h2-1);
            g.drawLine(w2-3,h2  ,w2+2,h2  );
            g.drawLine(w2-2,h2+1,w2+1,h2+1);
            g.drawLine(w2-1,h2+2,w2  ,h2+2);

            g.translate(-(x+w-WIDTH),-(y-1));
        }

        public Insets getBorderInsets(Component c){
            return new Insets(0,0,0,WIDTH);
        }
    }
    
    protected void processMouseEvent(MouseEvent evt){
        if(isEnabled()==false){
            return;
        }
        switch(evt.getID()) {
            case MouseEvent.MOUSE_PRESSED:
                Border border = getBorder();
                Insets insets = border.getBorderInsets(this);
                if(evt.getX() >= getWidth() - insets.right) {
                	clickDropDown=true;
                }
                else{
                	clickDropDown=false;
                }
                super.processMouseEvent(evt);
                break;
            case MouseEvent.MOUSE_EXITED:
               // setCursor(Cursor.getDefaultCursor());
                super.processMouseEvent(evt);
                break;
            default:
                super.processMouseEvent(evt);
                break;
        }
    }
}

// 使用例
//jButtonSingleScale = new JButtonWithMenu();
//jButtonSingleScale.setHorizontalAlignment(SwingConstants.LEFT);
//jButtonSingleScale.setBounds(new Rectangle(12, 160, 85, 24));
//jButtonSingleScale.setText("Single");
//jButtonSingleScale.setToolTipText("Adjust amplitude scale to single scale (S)");
//jButtonSingleScale.addActionListener(new java.awt.event.ActionListener() {
//	public void actionPerformed(java.awt.event.ActionEvent e) {
//		// メニュー領域が押された
//		if(jButtonSingleScale.isClickedDropDown()){
//			JPopupMenu popup=new JPopupMenu();
//			JMenuItem mi = new JMenuItem("Sigle Scale");
//			mi.addActionListener(new ActionListener(){
//				@Override
//				public void actionPerformed(ActionEvent arg0) {
//					GraphicView.doSingleScale();
//				}
//				
//			});
//	        popup.add(mi);
//	        popup.addSeparator();
//			JMenuItem mi2 = new JMenuItem("My scale...");
//			mi2.setIcon(new ImageIcon(getClass().getClassLoader().getResource("images/add.png")));
//			mi2.addActionListener(new ActionListener(){
//				@Override
//				public void actionPerformed(ActionEvent arg0) {
//					try{
//						String s;
//						if(MyFavotiteAmplitude.isEmpty()) s="";
//						else s = MyFavotiteAmplitude.get(0);
//						String imp = JOptionPane.showInputDialog(null,(Object)s,"Enter scale (peek to peek)",JOptionPane.PLAIN_MESSAGE);
//						long val = Long.valueOf(imp);
//						if(val<=0){
//							return;
//						}
//						GraphicView.doSingleScale(val);
//						for(int i=0;i<MyFavotiteAmplitude.size();i++){
//							String ss = MyFavotiteAmplitude.get(i);
//							if(ss.equals(imp)){
//								MyFavotiteAmplitude.remove(i);
//								break;
//							}
//						}
//						MyFavotiteAmplitude.add(0,imp);
//						if(MyFavotiteAmplitude.size()>5) MyFavotiteAmplitude.remove(5);
//					} catch(NumberFormatException e){
//						
//					}
//				}
//			});
//	        popup.add(mi2);
//	        for(int i=0;i<MyFavotiteAmplitude.size();i++){
//	        	JMenuItem mi3 = new JMenuItem(MyFavotiteAmplitude.get(i));
//				mi3.addActionListener(new ActionListener(){
//					@Override
//					public void actionPerformed(ActionEvent arg0) {
//						try{
//							String cmd = arg0.getActionCommand();
//							long val = Long.valueOf(cmd);
//							if(val<=0) return;
//							GraphicView.doSingleScale(val);
//						}catch(NumberFormatException e){
//							
//						}
//					}
//				});		
//				popup.add(mi3);
//	        }
//	        
//	        
//	        popup.show(jButtonSingleScale,0,jButtonSingleScale.getHeight());
//			
//		}
//		else{
//			GraphicView.doSingleScale();
//		}
//	}
//});
//}
